var latsign = 1;
var lonsign = 1;
var absdlat = 0;
var absdlon = 0;
var absmlat = 0;
var absmlon = 0;
var absslat = 0;
var absslon = 0;
	
function convertGeo()
{	
	setDlat();
	setMlat();
	setSlat();
	setDlon();
	setMlon();
	setSlon();

     var lat = Math.round(absdlat + (absmlat/60.) + (absslat/3600.) ) * latsign/1000000;
     var lng = Math.round(absdlon + (absmlon/60) + (absslon/3600) ) * lonsign/1000000;
     
     $("#lat").val("-" + lat);
     $("#lng").val("-" + lng);
	 latsign=1;
	 lonsign=1;
}

function setDlat()
{
	if($("#dlat").val() < 0)  
	{ 
		latsign = -1;
	}
	
	absdlat = Math.abs( Math.round($("#dlat").val() * 1000000.));
	
		 //Math.round is used to eliminate the small error caused by rounding in the computer:
		 //e.g. 0.2 is not the same as 0.20000000000284
	
	     //Error checks
	if(absdlat > (90 * 1000000)) 
	{ 
		alert(' Degrees Latitude must be in the range of -90 to 90. ');
		$("#dlat").val("");  
		$("#dlat").val(""); 
		$("#mlat").val("");		
	}
}

function setMlat()
{

    $("#mlat").val(Math.abs(Math.round($("#mlat").val() * 1000000.)/1000000));  //integer
    absmlat = Math.abs(Math.round($("#mlat").val() * 1000000.));  //integer

     //Error checks
     if(absmlat >= (60 * 1000000)) {  alert(' Minutes Latitude must be in the range of 0 to 59. ');   $("#mlat").val(""); $("#slat").val(""); }

}

function setSlat()
{

     $("#slat").val(Math.abs(Math.round($("#slat").val() * 1000000.)/1000000));
     absslat = Math.abs(Math.round($("#slat").val() * 1000000.));         // Note: kept as big integer for now, even if submitted as decimal

     //Error checks
     if(absslat > (59.99999999 * 1000000)) {  alert(' Minutes Latitude must be 0 or greater \n and less than 60. ');   $("#slat").val(""); }

}

function setDlon()
{
      if($("#dlon").val() < 0)  { lonsign = -1; }
      absdlon = Math.abs( Math.round($("#dlon").val() * 1000000.));

	 //Math.round is used to eliminate the small error caused by rounding in the computer:
	 //e.g. 0.2 is not the same as 0.20000000000284

     //Error checks
     if(absdlon > (180 * 1000000)) {  alert(' Degrees Longitude must be in the range of -180 to 180. '); $("#dlon").val(""); $("#mlon").val("");}

}

function setMlon()
{


     $("#mlon").val(Math.abs(Math.round($("#mlon").val() * 1000000.)/1000000));  //integer
     absmlon = Math.abs(Math.round($("#mlon").val() * 1000000));  //integer
     //Error checks
     if(absmlon >= (60 * 1000000))   {  alert(' Minutes Longitude must be in the range of 0 to 59. ');   $("#mlon").val(""); $("#slon").val(""); }

}

function setSlon()
{
     $("#slon").val(Math.abs(Math.round($("#slon").val() * 1000000.)/1000000));
     absslon = Math.abs(Math.round($("#slon").val() * 1000000.));         // Note: kept as big integer for now, even if submitted as decimal

     //Error checks
     if(absslon > (59.99999999 * 1000000)) {  alert(' Minutes Latitude must be 0 or greater \n and less than 60. ');   $("#slon").val(""); }
}